package com.yuyu.commons.config;

import com.yuyu.commons.security.AccessLimitInterceptor;
import com.yuyu.commons.security.AuthenticationInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author
 * @version 1.0
 * @description: TODO
 * @date 2022/8/3 17:44
 */
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    //这里需要注入，拦截器在生效前，拦截器里的bean并未生成注入，会导致redisUtil空指针异常
    @Resource
    private AccessLimitInterceptor accessLimitInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOrigins("*")
                // 是否允许证书 不再默认开启
                .allowCredentials(false)
                // 设置允许的方法
                .allowedMethods("*")
                // 跨域允许时间
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //可以添加多个
        registry.addInterceptor(new AuthenticationInterceptor());
        registry.addInterceptor(accessLimitInterceptor);
    }
}
